<template>
  <div class="employees">
    <!-- 工具栏组件 -->
    <mytools>
      <template #right>
        <el-button size="mini" type="primary" @click="toimport">导入</el-button>
        <el-button size="mini" type="primary" @click="openAdd"
          ><i class="el-icon-plus"></i>新增员工</el-button
        >
      </template>
    </mytools>
    <!-- 表格数据 -->
    <el-card class="mycard">
      <el-table :data="list">
        <el-table-column
          label="序号"
          type="index"
          fixed="left"
        ></el-table-column>
        <el-table-column
          label="姓名"
          width="200"
          prop="username"
        ></el-table-column>
        <el-table-column label="头像" width="200">
          <template slot-scope="{ row }">
            <img
              @click="changeEwm(row.staffPhoto)"
              :src="row.staffPhoto"
              v-myImg="defaultImg"
              alt=""
              class="myimg"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="手机号"
          width="200"
          prop="mobile"
        ></el-table-column>
        <el-table-column
          label="工号"
          width="200"
          prop="workNumber"
        ></el-table-column>
        <el-table-column label="聘用形势" width="200" prop="formOfEmployment">
          <template slot-scope="{ row }">
            {{ row.formOfEmployment | myEmployees }}
          </template>
        </el-table-column>
        <el-table-column
          label="部门"
          width="200"
          prop="departmentName"
        ></el-table-column>
        <el-table-column label="入职时间" width="200">
          <template slot-scope="{ row }">
            {{ row.timeOfEntry | mytimer }}
          </template>
        </el-table-column>
        <el-table-column label="是否在职" width="200">
          <template>
            <div>在职</div>
          </template>
        </el-table-column>
        <el-table-column label="状态" width="200">
          <template>
            <div>可用</div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="300" fixed="right">
          <template slot-scope="{ row }">
            <el-button type="text" @click="toDetail(row)">查看</el-button>
            <el-button type="text">转正</el-button>
            <el-button type="text">调岗</el-button>
            <el-button type="text">离职</el-button>
            <el-button type="text" @click="toRole(row)">角色</el-button>
            <el-button type="text">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        class="pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="pageSizes"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 新增、编辑面板 -->
    <panel :updata="getEmpList" :dialogShow.sync="dialogShow"></panel>
    <!-- 二维码面板 -->
    <el-dialog :visible.sync="showEwm" class="myDia">
      <!-- 添加转换后的显示容器 -->
      <canvas ref="myCanvas" class="ewm" />
    </el-dialog>
    <!-- 分配角色的弹窗 -->
    <el-dialog :visible.sync="showRole" title="分配角色">
      <el-form>
        <el-form-item>
          <!-- 复选框组 -->
          <el-checkbox-group v-model="checkList">
            <el-checkbox
              :label="item.id"
              v-for="(item, index) in roleList"
              :key="index"
              >{{ item.name }}</el-checkbox
            >
          </el-checkbox-group>
        </el-form-item>
        <el-form-item class="mybtn">
          <el-button type="primary" @click="submit" size="small"
            >提交</el-button
          >
          <el-button @click="closeDialog">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
//导入获取所有的员工列表数据
import { getEmpAllList, getEmpoloyeesApi ,setEmpRoleApi} from '@/api/employees'
import { getRoleList} from '@/api/setting'
//导入生成二维码插件
import QrCode from 'qrcode'
export default {
  data () {
    return {
      // 员工列表数据源
      list: [],
      // 当前页
      currentPage: 1,
      // 页容量的集合
      pageSizes: [8, 16, 24, 32],
      // 当前页容量
      pageSize: 8,
      // 总条数
      total: 0,
      //控制弹出面板的显示与隐藏
      dialogShow: false,
      //当图片加载失败时，使用默认图片
      defaultImg: require('@/assets/common/01.png'),
      //控制二维码的显示与隐藏
      showEwm: false,
      //控制角色弹窗的显示与隐藏
      showRole: false,
      //分配角色的多选组的数据
      checkList: [],
      //角色列表
      roleList: [],
      // 当前操作的员工
      currentObj: {}
    }
  },
  methods: {
    //点击 确定按钮，提交数据到服务器
    async submit () {
      await setEmpRoleApi({
        id: this.currentObj.id,
        roleIds: this.checkList
      })
      //提示修改成功
      this.$message.success('分配角色成功')
      //关闭面板
      this.showRole = false
      //更新数据
      this.getEmpList()
    },
    //获取所有的角色列表
    async getRoList () {
      const res = await getRoleList()
      //绑定数据到角色列表中
      this.roleList = res.data.rows
    },
    //点击取消按钮关闭分配角色的弹窗
    closeDialog () {
      this.showRole = false
    },
    //点击角色按钮，弹出弹窗
    async toRole (row) {
      // 保存当前操作的对象
      this.currentObj = row
      this.showRole = true
      const res = await getEmpoloyeesApi(row.id)
      //将当前员工的角色信息保存起来
      this.checkList = res.data.roleIds
    },
    //点击头像，弹出对话框
    async changeEwm (img) {
      this.showEwm = true
      await this.$nextTick(() => {
        //初始化 qrcode 对象
        QrCode.toCanvas(this.$refs.myCanvas, img, {
          width: 200,
          height: 200
        })
      })
    },
    //点击查看按钮，跳转到详情页
    toDetail (row) {
      this.$router.push(`/employees/detail/${row.id}`)
    },
    //点击导入按钮，跳转到批量导入页
    toimport () {
      this.$router.push('/employees/import')
    },
    //点击新增按钮触发的事件
    openAdd () {
      this.dialogShow = true
    },
    //封装获取所有员工列表的数据方法
    async getEmpList () {
      const res = await getEmpAllList({
        //页码
        page: this.currentPage,
        //每页条数
        size: this.pageSize
      })
      //  console.log(res);
      //绑定数据
      this.list = res.data.rows
      //绑定总条数
      this.total = res.data.total
    },
    // 页容量改变时触发
    handleSizeChange (size) {
      //修改页容量
      this.pageSize = size
      //重新请求数据
      this.getEmpList()
    },
    // 当前页改变时触发
    handleCurrentChange (page) {
      //修改当前页
      this.currentPage = page
      //重新请求数据
      this.getEmpList()
    }
  },
  created () {
    //页面一加载就渲染所有员工数据列表
    this.getEmpList()
    //获取角色列表
    this.getRoList()
  },
  components: {
    panel: () => import('./components/panel.vue')
  }
}
</script>

<style lang="scss">
.employees {
  margin: 20px;
  .mycard {
    margin-top: 20px;
  }
  .el-table__header {
    th:last-child {
      .cell {
        text-align: center;
      }
    }
  }
  .myimg {
    width: 50px;
    height: 50px;
  }
  .myDia {
    .ewm {
      text-align: center;
    }
  }
  .mybtn {
    text-align: center;
  }
}
</style>
